package reggie.ruiji.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import reggie.ruiji.mapper.EmployeeMapper;
import reggie.ruiji.pojo.Employee;
import reggie.ruiji.pojo.PageBean;
import reggie.ruiji.service.EmployeeService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements  EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;


    @Override
    public Employee selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    @Override
    public void insertEmployee(HttpServletRequest request, Employee employee) {

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Long empID=(Long)request.getSession().getAttribute("employee");

        employee.setCreateUser(empID);
        employee.setUpdateUser(empID);

        employeeMapper.insert(employee);

    }

    @Override
    public PageBean pagee(Integer pageNo, Integer pageSize,String name) {

        List<Employee> emplist= employeeMapper.list(name);
        PageBean pageBean = new PageBean(emplist,emplist.size(),pageSize,pageNo);
        return pageBean;
    }

    @Override
    public void update(Employee employee, HttpServletRequest request) {
        employee.setUpdateTime(LocalDateTime.now());
        Long id=(Long)request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employeeMapper.updateByid(employee);

    }

    @Override
    public Employee selectById(Integer id) {
        Employee employee=employeeMapper.selectById(id);
        return employee;
    }


}
